package checkauto.camera.com;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import checkauto.camera.com.util.AppManager;
import checkauto.camera.com.util.CameraParametersUtils;
import checkauto.camera.com.util.SharedPreferencesHelper;
import checkauto.camera.com.util.Utils;

import kernel.BusinessCard.android.RecogService;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;
import android.media.ToneGenerator;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import checkauto.camera.com.view.ViewfinderView;

/**
 * 
 * 项目名称：ViCard 类名称：CameraActivity 类描述： 相机界面 创建人：huangzhen 创建时间：2015-9-16
 * 上午9:10:57 修改人：huangzhen 修改时间：2015-9-16 上午9:10:57 修改备注：
 * 
 * @version
 * 
 */
public class CameraActivity extends Activity implements SurfaceHolder.Callback,
		Camera.PreviewCallback, OnClickListener {
	private SurfaceView surfaceView;
	private ViewfinderView myViewfinderView;
	private CameraParametersUtils cameraParametersutils;
	private int width, height, WIDTH, HEIGHT;
	private Camera camera;
	private TimerTask timer;
	private SurfaceHolder surfaceHolder;
	private ImageView iv_kernel_camera_back, iv_kernel_camera_flash,
			iv_kernel_camera;
	private ToneGenerator tone;

	public static String path = Environment.getExternalStorageDirectory()
			.toString() + "/wtimage/";
	private boolean isRecoging = false;
	private boolean isInitBusinessCard = false;
	private RecogService.recogBinder recogBinder;
	private List<String> focusModes;
	private static String selectPath = "";
	private Size size;
	private Parameters parameters;
	private int Format = ImageFormat.NV21;// .YUY2
	public static byte[] recogBytes;
	public static int[] recogInts;
	public int byteDataType = 0;
	private TextView tv_camera_info;
	public boolean isAutoRecog = false;// 如果分辨率符合要求就可以进行自动拍照，否则只能手动拍照
	private RecogOpera recogUtils;
	private int count = 0;// 连续检测到边线的次数
	private int COUNT = 2;// 常量
	public static int picwidth, picheiget;
	private Timer time;
	private boolean isFirstCreate = true;
	private Message msg;
	private boolean isOpenFlash = false;
	private byte[] date1;
	private boolean isTouched = false;
	private boolean isTackePic=false;
	private int sum=0;
	private int uiRot = 0, tempUiRot = 0, rotation = 0;
	private int SurfaceView, Tv_camera_info, MyViewfinderView, zoomin, zoomout,
			Iv_kernel_camera, Iv_kernel_camera_flash, Iv_kernel_camera_back,
			unsupport_auto_focus, toast_autofocus_failure, noFoundProgram,
			not_support_scan, unsupportflash, flash_off, flash_on,
			openCameraPermission_id;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			findView();
		};
	};
	public ServiceConnection recogConn = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
			recogConn = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			try {

				recogBinder = (RecogService.recogBinder) service;

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    	this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
	//	uiRot=getWindowManager().getDefaultDisplay().getRotation();
		//Log.i("string","旋转角度的数值为:"+uiRot);
		int autocamera_activity = getResources().getIdentifier(
				"bucard_camera_activity", "layout", this.getPackageName());
		setContentView(autocamera_activity);
		cameraParametersutils = new CameraParametersUtils(
				getApplicationContext());
		width = cameraParametersutils.srcWidth;
		height = cameraParametersutils.srcHeight;
		InitResouce();
		cameraParametersutils.hiddenVirtualButtons(getWindow().getDecorView());

		AppManager.getAppManager().addActivity(CameraActivity.this);
	}

	private void InitResouce() {
		// TODO Auto-generated method stub
		flash_off = getResources().getIdentifier("flash_off", "drawable",
				this.getPackageName());
		flash_on = getResources().getIdentifier("flash_on", "drawable",
				this.getPackageName());
		unsupportflash = getResources().getIdentifier("unsupportflash",
				"string", this.getPackageName());
		not_support_scan = getResources().getIdentifier("not_support_scan",
				"string", this.getPackageName());
		openCameraPermission_id = getResources().getIdentifier(
				"openCameraPermission", "string", this.getPackageName());
		SurfaceView = getResources().getIdentifier("surfaceView", "id",
				this.getPackageName());
		Tv_camera_info = getResources().getIdentifier("tv_camera_info", "id",
				this.getPackageName());
		unsupport_auto_focus = getResources().getIdentifier(
				"unsupport_auto_focus", "string", this.getPackageName());
		toast_autofocus_failure = getResources().getIdentifier(
				"toast_autofocus_failure", "string", this.getPackageName());
		noFoundProgram = getResources().getIdentifier("noFoundProgram",
				"string", this.getPackageName());
		MyViewfinderView = getResources().getIdentifier("myViewfinderView",
				"id", this.getPackageName());
		Iv_kernel_camera_back = getResources().getIdentifier(
				"iv_kernel_camera_back", "id", this.getPackageName());
		Iv_kernel_camera_flash = getResources().getIdentifier(
				"iv_kernel_camera_flash", "id", this.getPackageName());
		Iv_kernel_camera = getResources().getIdentifier("iv_kernel_camera",
				"id", this.getPackageName());
		zoomin = getResources().getIdentifier("zoomin", "anim",
				this.getPackageName());
		zoomout = getResources().getIdentifier("zoomout", "anim",
				this.getPackageName());
		surfaceView = (SurfaceView) this.findViewById(SurfaceView);
		tv_camera_info = (TextView) this.findViewById(Tv_camera_info);
		myViewfinderView = (ViewfinderView) this.findViewById(MyViewfinderView);
		iv_kernel_camera_back = (ImageView) this
				.findViewById(Iv_kernel_camera_back);
		iv_kernel_camera_flash = (ImageView) this
				.findViewById(Iv_kernel_camera_flash);
		iv_kernel_camera = (ImageView) this.findViewById(Iv_kernel_camera);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		// System.out.println("执行的方法为onStart方法!!!!!!!");
		isInitBusinessCard = false;
		Intent intent = getIntent();
		isAutoRecog = intent.getBooleanExtra("autocamera", false);
		SharedPreferencesHelper.putBoolean(getApplicationContext(),
				"isAutoRecog", isAutoRecog);
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(CameraActivity.this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		if (Utils.isCPUInfo64()) {
			System.out.println("64位CPU");
			COUNT = 2;
		} else {
			System.out.println("32位CPU");
			COUNT = 1;
		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		StartCamera();

		if (!isFirstCreate) {
			setParams();
		}
	}

	private void StartCamera() {
		// TODO Auto-generated method stub
		try {
			try {
				if (null == camera) {
					camera = Camera.open();
				}
			} catch (Exception e) {
				// 禁止使用相机权限后防止布局混乱
				Toast.makeText(getApplicationContext(),
						getString(openCameraPermission_id), Toast.LENGTH_SHORT)
						.show();
				msg = new Message();
				handler.sendMessage(msg);
			}

			isRecoging = false;
			if (time == null)
				time = new Timer();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Title: findView
	 * @Description: 相机界面UI布局
	 * @param :设定文件
	 * @return void 返回类型
	 * @throws
	 */
	private void findView() {
		// TODO Auto-generated method stub
		iv_kernel_camera.setOnClickListener(this);
		iv_kernel_camera_back.setOnClickListener(this);
		iv_kernel_camera_flash.setOnClickListener(this);
		// 返回按钮布局
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				(int) (width * 0.05), (int) (width * 0.05));
		params.leftMargin = (int) (width * 0.03);
		params.topMargin = (int) (height * 0.83);
		iv_kernel_camera_back.setLayoutParams(params);

		// 闪光灯布局
		params = new RelativeLayout.LayoutParams((int) (width * 0.05),
				(int) (width * 0.05));
		params.leftMargin = (int) (width * 0.03);
		params.topMargin = (int) (height * 0.09);
		iv_kernel_camera_flash.setLayoutParams(params);

		int surfaceWidth = cameraParametersutils.surfaceWidth;
		int surfaceHeight = cameraParametersutils.surfaceHeight;
		if (width == surfaceView.getWidth() || surfaceView.getWidth() == 0) {
			// 预览界面
			params = new RelativeLayout.LayoutParams(width, height);
			surfaceView.setLayoutParams(params);
			// 扫描框
			params = new RelativeLayout.LayoutParams(width, height);
			myViewfinderView.setLayoutParams(params);
			// 拍照按钮布局
			params = new RelativeLayout.LayoutParams((int) (width * 0.08),
					(int) (width * 0.08));
			params.leftMargin = (int) (width * 0.85);
			params.addRule(RelativeLayout.CENTER_VERTICAL);
			iv_kernel_camera.setLayoutParams(params);
			// 提示语布局
			params = new RelativeLayout.LayoutParams(width, height);
			tv_camera_info.setLayoutParams(params);
			tv_camera_info.setGravity(Gravity.CENTER);
		} else if (width > surfaceView.getWidth()) {
			// 如果将虚拟硬件弹出则执行如下布局代码，相机预览分辨率不变压缩屏幕的高度
			int surfaceViewHeight = (surfaceView.getWidth() * height) / width;
			params = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.FILL_PARENT, surfaceViewHeight);
			params.topMargin = (height - surfaceViewHeight) / 2;
			surfaceView.setLayoutParams(params);
			// 拍照按钮布局
			params = new RelativeLayout.LayoutParams((int) (width * 0.1),
					(int) (width * 0.1));
			params.addRule(RelativeLayout.CENTER_VERTICAL);
			params.leftMargin = (int) (width * 0.83);
			iv_kernel_camera.setLayoutParams(params);
		}
		if (surfaceWidth < width || surfaceHeight < height) {

			// 预览界面
			params = new RelativeLayout.LayoutParams(surfaceWidth,
					surfaceHeight);
			params.addRule(RelativeLayout.CENTER_IN_PARENT);
			surfaceView.setLayoutParams(params);
			// 扫描框
			params = new RelativeLayout.LayoutParams(surfaceWidth,
					surfaceHeight);
			params.addRule(RelativeLayout.CENTER_IN_PARENT);
			myViewfinderView.setLayoutParams(params);
			// 拍照按钮布局
			params = new RelativeLayout.LayoutParams((int) (width * 0.08),
					(int) (width * 0.08));
			params.leftMargin = (int) (width * 0.8);
			params.addRule(RelativeLayout.CENTER_VERTICAL);
			iv_kernel_camera.setLayoutParams(params);
			// 提示语布局
			params = new RelativeLayout.LayoutParams(surfaceWidth,
					surfaceHeight);
			params.addRule(RelativeLayout.CENTER_IN_PARENT);
			tv_camera_info.setLayoutParams(params);
			tv_camera_info.setGravity(Gravity.CENTER);
		}

		if (isAutoRecog) {
			// iv_kernel_camera.setVisibility(View.GONE);
			// 视频流模式 必须最先设置，因为他决定调用哪个初始化函数
			RecogService.byteDataType = 0;
		} else {
			// iv_kernel_camera.setVisibility(View.VISIBLE);
			// 拍照模式 必须最先设置，因为他决定调用哪个初始化函数
			RecogService.byteDataType = 1;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		// 返回按钮触发事件
		if (id == Iv_kernel_camera_back) {
			isTouched = true;
			sum = -1;
			if (time != null) {
				time.cancel();
				time = null;
			}
			finish();
		//	closeCamera();
//			Intent intent = new Intent(CameraActivity.this, ImageChooser.class);
//		//	CameraActivity.this.finish();
//			startActivity(intent);
		//	overridePendingTransition(zoomin, zoomout);
			// 拍照按钮触发事件
		} else if (id == Iv_kernel_camera) {

			if (camera!= null) {
				if (timer != null) {
					timer.cancel();
					timer = null;
				}
				isAutoRecog=false;
				isTackePic=true;
				RecogService.byteDataType = 1;
			}
			// 闪光灯按钮触发事件
		} else if (id == Iv_kernel_camera_flash) {
			try {
				if (camera == null)
					camera = Camera.open();
				Parameters parameters = camera.getParameters();
				List<String> flashList = parameters.getSupportedFlashModes();
				if (flashList != null
						&& flashList
								.contains(Parameters.FLASH_MODE_TORCH)) {
					if (!isOpenFlash) {
						iv_kernel_camera_flash.setBackgroundResource(flash_on);
						isOpenFlash = true;
						parameters
								.setFlashMode(Parameters.FLASH_MODE_TORCH);
						camera.setParameters(parameters);
					} else {
						iv_kernel_camera_flash.setBackgroundResource(flash_off);
						isOpenFlash = false;
						parameters
								.setFlashMode(Parameters.FLASH_MODE_OFF);
						camera.setParameters(parameters);
					}
				} else {
					Toast.makeText(getApplicationContext(),
							getString(unsupportflash), Toast.LENGTH_SHORT)
							.show();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.hardware.Camera.PreviewCallback#onPreviewFrame(byte[],
	 * android.hardware.Camera)
	 */
	@Override
	public void onPreviewFrame(byte[] data, Camera camera) {
		// TODO Auto-generated method stub
		if(isTouched){
			return;
		}else {
			if (sum == 0) {
				recogBytes = data;
				MyThread thread = new MyThread();
				thread.start();
			}
		}

      //  Log.i("string","数值"+i);
	}
	class MyThread extends Thread {
		public MyThread() {
			// TODO Auto-generated constructor stub
		}
		@Override
		public void run() {
			RecogOpera();
		}
	}
	private synchronized void RecogOpera(){
		sum = sum + 1;
		if (isTouched) {
			return;
		}
		if (!isInitBusinessCard) {
			Intent recogIntent = new Intent(getApplicationContext(),
					RecogService.class);
			bindService(recogIntent, recogConn, Service.BIND_AUTO_CREATE);
			isInitBusinessCard = true;
			size = camera.getParameters().getPreviewSize();
		}
		if (isAutoRecog) {

			boolean isCheckPicIsClear = false;
			boolean DetectSideLine = false;
			if (recogBinder != null) {
				recogBinder.SetCardROI((int) (size.width * 0.15),
						(int) (size.height - 0.41004673 * size.width) / 2,
						(int) (size.width * 0.8),
						(int) (size.height + 0.41004673 * size.width) / 2);
				int[] arrLinePos = new int[4];
				//long time = System.currentTimeMillis();
				DetectSideLine = recogBinder.ReturnDetectSideLine(recogBytes, WIDTH,
						HEIGHT, 12, arrLinePos, 4);
				if (DetectSideLine) {
					isCheckPicIsClear = recogBinder.ReturnCheckPicIsClear(recogBytes,
							size.width, size.height, 12);
					if (isCheckPicIsClear)
						count++;
				}
				if (isCheckPicIsClear && DetectSideLine && count == COUNT&&!isRecoging) {
					isRecoging=true;
					count = 0;
					// 样本保存功能start
					SavePicFullPath();
					new FrameCapture(recogBytes, size.width, size.height, "12");
					// end
					myViewfinderView.setCheckLeftFrame(1);
					myViewfinderView.setCheckTopFrame(1);
					myViewfinderView.setCheckRightFrame(1);
					myViewfinderView.setCheckBottomFrame(1);
					if (timer != null) {
						timer.cancel();
						timer = null;
					}
					camera.setPreviewCallback(null);
					camera.stopPreview();
					// 开始识别
					recogUtils = new RecogOpera(0, CameraActivity.this);
					recogUtils.startActivityRecog(recogBytes, size, WIDTH, HEIGHT,
							selectPath);
				}
			}

		}else{
			if (!isRecoging&&isTackePic) {
				isRecoging=true;
				isTackePic=false;
				camera.takePicture(null, null, PictureCallback);
			}
		}
		sum = sum - 1;
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
	}

	@Override
	public void onDestroy() {

		// TODO Auto-generated method stub
		super.onDestroy();
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		closeCamera();
		if (recogBinder != null) {
			unbindService(recogConn);
			recogBinder = null;
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

		// TODO Auto-generated method stub

		if (camera != null) {
			setParams();
			if (isFirstCreate) {
				isFirstCreate = false;
				CameraActivity.this.runOnUiThread(updateUI);
			} else {
				msg = new Message();
				handler.sendMessage(msg);

			}
		}
	}

	private void setParams() {
		// TODO Auto-generated method stub
		if (camera != null) {
		//	camera.setDisplayOrientation(0);
			cameraParametersutils.getCameraPreParameters(camera);
			WIDTH = cameraParametersutils.preWidth;
			HEIGHT = cameraParametersutils.preHeight;
			parameters = camera.getParameters();
			if (parameters.getSupportedFocusModes().contains(
					Parameters.FOCUS_MODE_AUTO)) {
				parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
			}
			parameters.setPictureFormat(PixelFormat.JPEG);

			if (WIDTH >= 1920 && HEIGHT >= 1080) {
				parameters.setPreviewSize(WIDTH/* 1920 */, HEIGHT/* 1080 */);
			} else {
				parameters.setPreviewSize(WIDTH/* 1920 */, HEIGHT/* 1080 */);
				// 如果不支持扫描模式，拍照按钮会自动显示出来，进行拍照识别
				iv_kernel_camera.setVisibility(View.VISIBLE);
				// 拍照模式 必须最先设置，因为他决定调用哪个初始化函数
				RecogService.byteDataType = 1;
				isAutoRecog = false;
				Toast.makeText(this, not_support_scan, Toast.LENGTH_SHORT)
						.show();
			}
			try {
				camera.setPreviewDisplay(surfaceHolder);
			//	camera.setDisplayOrientation(rotation);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			camera.setPreviewCallback(CameraActivity.this);
			camera.setParameters(parameters);
			camera.startPreview();
			if (timer != null) {
				timer.cancel();
				timer = null;
			}
			if (time != null) {
				timer = new TimerTask() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						try {
							autoCameraFocus();
						} catch (Exception e) {
							// TODO: handle exception

						}
					}
				};
				time.schedule(timer, 200, 2500);
			}
		}
	}

	private Runnable updateUI = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			findView();
		}
	};

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		// 获得Camera对象
		// System.out.println("surfaceview执行方法为oncreate方法");

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// System.out.println("surfaceview执行方法为surfaceDestroyed方法");
		// TODO Auto-generated method stub
		synchronized (this) {
			try {
				if (camera != null) {
					camera.setPreviewCallback(null);
					camera.stopPreview();
					camera.release();
					camera = null;
				}
			} catch (Exception e) {
				Log.i("TAG", e.getMessage());
			}
		}
	}

	public void autoCameraFocus() {

		if (camera != null) {
			synchronized (camera) {
				try {
					if (camera.getParameters().getSupportedFocusModes() != null
							&& camera
									.getParameters()
									.getSupportedFocusModes()
									.contains(Parameters.FOCUS_MODE_AUTO)) {
						camera.autoFocus(new AutoFocusCallback() {
							public void onAutoFocus(boolean success,
									Camera camera) {

								if (success) {
									isRecoging = false;
								} else {
								}
							}
						});
					} else {
						Toast.makeText(getBaseContext(),
								getString(unsupport_auto_focus),
								Toast.LENGTH_LONG).show();
					}

				} catch (Exception e) {
					Looper.prepare();
					Toast.makeText(this, toast_autofocus_failure,
							Toast.LENGTH_SHORT).show();
					camera.stopPreview();
					camera.startPreview();
					isRecoging=false;
                     Looper.loop();

				}
			}
		}
	}

	public void closeCamera() {
		synchronized (this) {
			try {
				if (camera != null) {
					camera.setPreviewCallback(null);
					camera.stopPreview();
					camera.release();
					camera = null;
				}
			} catch (Exception e) {
				Log.i("TAG", e.getMessage());
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			isTouched = true;
			sum = -1;
			if (time != null) {
				time.cancel();
				time = null;
			}
		//	closeCamera();
		//	CameraActivity.this.finish();
			finish();
			overridePendingTransition(zoomin, zoomout);
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	/* 拍照后回显 */
	private PictureCallback PictureCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, Camera camera) {
			camera.setPreviewCallback(null);
			camera.stopPreview();

		//	isTouched=false;
			SavePicFullPath();
			try {
				recogUtils = new RecogOpera(1, CameraActivity.this);
				recogUtils.startActivityRecog(null, size, WIDTH, HEIGHT,
						selectPath);
			}catch (Exception e){
				Toast.makeText(getApplicationContext(),
						getString(noFoundProgram) + "kernel.bucard", 0).show();
				e.printStackTrace();
			}

		}

	};
	// 快门按下的时候onShutter()被回调拍照声音
	private ShutterCallback shutterCallback = new ShutterCallback() {
		public void onShutter() {
			if (tone == null)
				// 发出提示用户的声音
				tone = new ToneGenerator(1,// AudioManager.AUDIOFOCUS_REQUEST_GRANTED
						ToneGenerator.MIN_VOLUME);
			tone.startTone(ToneGenerator.TONE_PROP_BEEP);
		}
	};

	/*
	 * 拍照函數
	 */
	/* 拍照对焦 */
	/* 拍照 */
	public void takePicture() {

		if (camera != null) {
			try {
				if (focusModes != null
						&& focusModes
								.contains(Parameters.FOCUS_MODE_AUTO)) {
					camera.autoFocus(new AutoFocusCallback() {
						public void onAutoFocus(boolean success, Camera camera) {
							// if (success)
							// camera.stopPreview();
							// camera.startPreview();
							if (success) {

								camera.takePicture(shutterCallback, null,
										PictureCallback);
							} else {

								camera.takePicture(shutterCallback, null,
										PictureCallback);
							}

						}
					});
				} else {
					camera.takePicture(shutterCallback, null, PictureCallback);
				}
			} catch (Exception e) {
				// e.printStackTrace();
				camera.stopPreview();
				camera.startPreview();
				Toast.makeText(this, toast_autofocus_failure,
						Toast.LENGTH_SHORT).show();

			}
		}
	}

	/**
	 * 获取返回结果进行
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == 8 && resultCode == RESULT_OK) {
			// 读识别返回值
			Bundle bun = data.getBundleExtra("GetRecogResult");
			Intent intent = new Intent(CameraActivity.this, BucardRunner.class);
			intent.putExtra("RecogValue", bun);
			intent.putExtra("camera", isAutoRecog);
			CameraActivity.this.finish();
			startActivity(intent);
			overridePendingTransition(zoomin, zoomout);
		}
	}

    private  void SavePicFullPath(){
		  YuvImage yuvimage=null;
		  try {
			  selectPath = path + Utils.pictureName() + ".jpg";
			  File file = new File(path);
			  if (!file.exists())
				  file.mkdirs();
			  File file1 = new File(selectPath);
			  Utils.freeFileLock(new FileOutputStream(selectPath)
					  .getChannel().tryLock(), file1);

			  if("Nexus 5X".equals(Build.MODEL)){
				  byte[] nv21_new=Utils.rotateYUV420Degree180(recogBytes,size.width,size.height);
				  yuvimage = new YuvImage(nv21_new, ImageFormat.NV21,
						  size.width, size.height, null);
			  }else{
				  yuvimage = new YuvImage(recogBytes, ImageFormat.NV21,
						  size.width, size.height, null);
			  }
			  ByteArrayOutputStream baos = new ByteArrayOutputStream();
			  yuvimage.compressToJpeg(new Rect(0,
							  0,
							  size.width,
							  size.height), 100,
					  baos);
			  FileOutputStream outStream;
			  outStream = new FileOutputStream(selectPath);
			  outStream.write(baos.toByteArray());
			  outStream.close();
			  baos.close();

		  } catch (Exception e) {

		  }
	}
}
